package com.bridgetek.carlauncher.fragment;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.media.AudioManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bridgetek.carlauncher.Constants;
import com.bridgetek.carlauncher.MainActivity;
import com.bridgetek.carlauncher.R;

import java.util.List;


public class SecondFragment extends Fragment implements View.OnClickListener {
    private View mContentView;
    private MainActivity mActivity;

    public static SecondFragment newInstance() {
        Bundle args = new Bundle();
        SecondFragment fragment = new SecondFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_two_lay, container, false);
        return mContentView;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initListener();
    }

    private void initView() {
        mActivity = (MainActivity) getActivity();
    }

    private void initListener() {
        mContentView.findViewById(R.id.tv_music).setOnClickListener(this);
        mContentView.findViewById(R.id.tv_wifi).setOnClickListener(this);
        mContentView.findViewById(R.id.tv_reduce_volume).setOnClickListener(this);
        mContentView.findViewById(R.id.tv_add_volume).setOnClickListener(this);
        mContentView.findViewById(R.id.tv_playback).setOnClickListener(this);
        mContentView.findViewById(R.id.tv_setting).setOnClickListener(this);
        mContentView.findViewById(R.id.tv_fm).setOnClickListener(this);
        mContentView.findViewById(R.id.tv_about).setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_music:
                openMusic();
                break;
            case R.id.tv_wifi:
                connectWifi();
                break;
            case R.id.tv_reduce_volume:
                reduceVolume();
                break;
            case R.id.tv_add_volume:
                addVolume();
                break;
            case R.id.tv_playback:
                openPlayBack();
                break;
            case R.id.tv_setting:
                openSettings();
                break;
            case R.id.tv_fm:
                openFM();
                break;
            case R.id.tv_about:
                openProductInfo();
                break;
        }
    }

    /**
     * 打开在线音乐---酷我音乐
     */
    private void openMusic() {
        ResolveInfo resolveInfo = getResolveInfo(Constants.KUWO_PACKAGE);
        if (resolveInfo != null) {
            openApp(resolveInfo);
        } else {
            Toast.makeText(mActivity, mActivity.getString(R.string.kuwo_hint), Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * wifi链接
     */
    private void connectWifi() {
        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
        startActivity(intent);
    }

    /**
     * 减小音量--铃声、媒体音量、通话音量
     */
    private void reduceVolume() {
        AudioManager mAudioManager = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE);
        int currentV = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);

        if (0 == currentV || 1 == currentV) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        } else {
            //铃声
            mAudioManager.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
            //通话声音
            mAudioManager.adjustStreamVolume(AudioManager.STREAM_VOICE_CALL, AudioManager.ADJUST_LOWER, 0);
            //媒体声音
            mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, 0);
        }
    }

    /**
     * 增加音量--铃声、媒体音量、通话音量
     */
    private void addVolume() {
        AudioManager mAudioManager = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE);
        int currentV = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
        if (0 == currentV) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }

        //铃声
        mAudioManager.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
        //通话声音
        mAudioManager.adjustStreamVolume(AudioManager.STREAM_VOICE_CALL, AudioManager.ADJUST_RAISE, 0);
        //媒体声音
        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);
    }

    /**
     * 回放
     */
    private void openPlayBack() {
        ResolveInfo resolveInfo = getResolveInfo(Constants.GALLERY3D_PACKAGE);
        if (resolveInfo != null) {
            openApp(resolveInfo);
        } else {
            Toast.makeText(mActivity, mActivity.getString(R.string.gallery3d_hint), Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 打开设置界面
     */
    private void openSettings() {
        Intent intent = new Intent(Settings.ACTION_SETTINGS);
        startActivity(intent);
    }

    /**
     * FM发射
     */
    private void openFM() {
        ResolveInfo resolveInfo = getResolveInfo(Constants.FM_TRANSMITTER_PACKAGE);
        if (resolveInfo != null) {
            openApp(resolveInfo);
        } else {
            Toast.makeText(mActivity, mActivity.getString(R.string.fm_hint), Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 产品信息
     */
    private void openProductInfo() {
        Intent intent = new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS);
        startActivity(intent);
    }


    /**
     * 判断应用是否存在,若存在则获取其ResolveInfo对象
     *
     * @param pack
     * @return
     */
    private ResolveInfo getResolveInfo(String pack) {
        if (mActivity != null) {
            //应用过滤条件
            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            PackageManager mPackageManager = mActivity.getPackageManager();
            List<ResolveInfo> mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);

            for (ResolveInfo res : mAllApps) {
                //该应用的包名和主Activity
                if (res.activityInfo.packageName.equals(pack)) {
                    return res;
                }
            }
        }
        return null;
    }

    /**
     * 打开应用
     *
     * @param res
     */
    private void openApp(ResolveInfo res) {
        if (res != null) {
            String pkg = res.activityInfo.packageName;
            String cls = res.activityInfo.name;
            ComponentName component = new ComponentName(pkg, cls);
            Intent intent = new Intent();
            intent.setComponent(component);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mActivity.startActivity(intent);
        }

    }
}
